/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Hashtable;

/**
 *
 * @author Kvest
 */
public class SettingsLoader {
    public static final String SETTINGS_FILE_NAME = "/settings/settings.txt";
    public static final String IP_KEY = "ip";
    public static final String PORT_KEY = "port";
    public static final String LOGIN_KEY = "login";  
    public static final String PASSWORD_KEY = "password";
    public static final String CALLSIGN_KEY = "callsign";
    
    private static final String SEPARATOR = ":";
    
    private Hashtable items;
    
    public SettingsLoader() {
        items = new Hashtable();
    }
    
    public void load(String fileName) {
        //clear old values
        items.clear();
        
        //load new values
        InputStream is = getClass().getResourceAsStream(fileName);
        if (is == null) {
            return;
        }
        
        InputStreamReader reader;
        try {
            
            reader = new InputStreamReader(is, "utf-8");
        } catch (UnsupportedEncodingException uee) {
            reader = new InputStreamReader(is);
        }
        String line = null;
        try {
            // Read a single line from the file. null represents the EOF.
            while ((line = readLine(reader)) != null) {
                // Append the read line to the main form with a linefeed ('\n')
                processItem(line);
            }        
            reader.close();
        } catch(IOException ioe) {} 
    }
    
    private String readLine(InputStreamReader reader) throws IOException {
        // Test whether the end of file has been reached. If so, return null.
        int readChar = reader.read();
        if (readChar == -1) {
            return null;
        }
        StringBuffer string = new StringBuffer("");
        // Read until end of file or new line
        while (readChar != -1 && readChar != '\n') {
            // Append the read character to the string. Some operating systems
            // such as Microsoft Windows prepend newline character ('\n') with
            // carriage return ('\r'). This is part of the newline character
            // and therefore an exception that should not be appended to the
            // string.
            if (readChar != '\r') {
                string.append((char)readChar);
            }
            // Read the next character
            readChar = reader.read();
        }
        return string.toString();
    }
    
    private void processItem(String item) {
        if (item == null) {
            return;
        }
        int separatorIndex = item.indexOf(SEPARATOR);
        if (separatorIndex == -1) {
            return;
        }
        
        String key = item.substring(0, separatorIndex).trim();
        String value = item.substring(separatorIndex + 1).trim();
        
        if (!key.equals("")) {
            items.put(key, value);
        }
    }
    
    public String getItem(String key, String defaultValue) {
        if (items.containsKey(key)) {
            return (String)items.get(key);
        } else {
            return defaultValue;
        }
    }
    
    public String getItem(String key) {
        return getItem(key, "");
    }
}
